const {
  getUserInfo,
  setUserAddress,
  setGender,
  getRegion
} = require('../../../api/api.js');
const {
  _navigateTo,
  _showToast
} = require('../../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listData: {},
    sexIndex: -1,
    sex: ["女", "男"],
    onlyArray: [
      [],
      [],
      []
    ],
    areaIndex: [0, 0, 0]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    getUserInfo().then(res => {
      this.setData({
        listData: res.data,
        sexIndex: res.data.gender
      })
    }).catch((err) => {
      console.log(err)
    });
    this.initArea().then(() => {
      const list = this.data.onlyArray;
      this.setData({
        onlyArray: list
      })
    });
  },
  bindCustomPickerChange(e) {
    const [province, city, county] = e.detail.value;
    console.log(province, city, county)
    const provinceCode = this.data.onlyArray[0][province] && this.data.onlyArray[0][province].region_code
    const cityCode = this.data.onlyArray[1][city] && this.data.onlyArray[1][city].region_code
    const countyCode = (this.data.onlyArray[2][county] && this.data.onlyArray[2][county].region_code);
    setUserAddress(provinceCode, cityCode, countyCode).then(() => {
      getUserInfo().then(res => {
        this.setData({
          listData: res.data
        })
      }).catch((err) => {
        console.log(err)
      });
    })
  },
  bindCustomPickerColumnChange(e) {
    const column = e.detail.column;
    const index = e.detail.value;
    if (column === 0) {
      const currentProvince = this.data.onlyArray[0][index].region_code;
      this.getCity(currentProvince).then(() => {
        this.setData({
          [`onlyArray[${1}]`]: this.data.onlyArray[1],
          [`onlyArray[${2}]`]: this.data.onlyArray[2],
        })
      });
    } else if (column === 1) {
      const currentCity = this.data.onlyArray[1][index].region_code;
      this.getCounty(currentCity).then(() => {
        this.setData({
          [`onlyArray[${2}]`]: this.data.onlyArray[2]
        })
      });
    }
  },
  initArea() {
    return new Promise((resolve, reject) => {
      getRegion().then(res => {
        const province = res.data;
        this.data.onlyArray[0] = province;
        const firstProvinceCode = province[0].region_code;
        this.getCity(firstProvinceCode).then(() => {
          resolve()
        }).catch(() => {
          reject()
        });
      })
    })
  },
  getCity(provinceRegion) {
    return new Promise((resolve, reject) => {
      getRegion(provinceRegion).then(city_res => {
        const city = city_res.data;
        this.data.onlyArray[1] = city;
        const firstCityCode = city[0].region_code;
        this.getCounty(firstCityCode).then(() => {
          resolve()
        }).catch(() => {
          reject()
        });
      })
    })
  },
  getCounty(cityRegion) {
    return getRegion(cityRegion).then(county_res => {
      const county = county_res.data;
      this.data.onlyArray[2] = county;
    })
  },

  /**
   *  用户地址信息
   */
  bindRegionChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    const PROVINCE = e.detail.value[0]; //省份信息
    const CITY = e.detail.value[1]; //城市信息
    const AREA = e.detail.value[2]; //城市城区信息
    setUserAddress(PROVINCE, CITY, AREA).then(res => {
      _showToast(res.msg)
      this.setData({
        regionProvince: PROVINCE,
        regionCity: CITY,
        regionArea: AREA,
      })
    }).catch(err => {
      console.log(err)
    })
  },
  bindSelectorChange: function(e) {
    const index = e.detail.value;
    setGender(index).then(res => {
      this.setData({
        sexIndex: index
      })
      _showToast(res.msg)
    }).catch(err => {
      console.log(err)
    })
  },
  /**
   * 跳转到更换手机号页面
   */
  toChangePhone(e) {
    //传递当前页面手机号到 change-phone 页面
    const {
      phone
    } = e.currentTarget.dataset;
    _navigateTo(`../change-phone/index?phone=${phone}`);
  }
})